#include "bga/graphics/SceneNode.h"

#include <cmath>

using namespace std;

namespace bga
{

SceneNode::SceneNode() : Node<SceneNodeElement>()
{
    SceneNodeElement* element = new SceneNodeElement();
    element->owner = this;

    this->setElement(*element);
    for(unsigned int i = 0; i < 4; i++)
    {
        for(unsigned int j = 0; j < 4; j++)
        {
            this->element->transformations(i, j) = (i == j) ? 1 : 0;
        }
    }
    /**this->element->transformations[0] = 1.f;
    this->element->transformations[1] = 0.f;
    this->element->transformations[2] = 0.f;
    this->element->transformations[3] = 0.f;
    this->element->transformations[4] = 0.f;
    this->element->transformations[5] = 1.f;
    this->element->transformations[6] = 0.f;
    this->element->transformations[7] = 0.f;
    this->element->transformations[8] = 0.f;
    this->element->transformations[9] = 0.f;
    this->element->transformations[10] = 1.f;
    this->element->transformations[11] = 0.f;
    this->element->transformations[12] = 0.f;
    this->element->transformations[13] = 0.f;
    this->element->transformations[14] = 0.f;
    this->element->transformations[15] = 1.f;*/

    this->dirtyDrawable = true;
    this->dirtyProgram = true;
    this->dirtyFbo = true;
    this->dirtyAlternativeFbo = true;

    this->postProcess = false;

    this->inheritParentTransformation = true;
    this->reflect = false;

    /// a SceneNode has a SceneNodeListener
    this->addNodeListener(listener);
}

SceneNode::~SceneNode()
{
    if(this->element != NULL)
    {
        delete this->element;
        this->element = NULL;
    }
}

void SceneNode::update()
{
    /// configure drawable VBO
    if (this->element->drawable != NULL && this->dirtyDrawable)
    {
        this->element->drawable->createAndConfigureVBOs();
        this->dirtyDrawable = false;
    }

    /// link program shader
    if (this->element->program != NULL && this->dirtyProgram)
    {
        this->element->program->link();
        this->dirtyProgram = false;
    }

    /// apply texture parameters
    for (unsigned int i = 0; i < this->element->texture.size(); i++)
    {
        if (this->dirtyTexture[i] && !(this->element->texture[i]->isImageLoaded()))
        {
            this->element->texture[i]->bind();
            this->element->texture[i]->applyParameters();
            this->element->texture[i]->loadImage();
            this->element->texture[i]->unbind();
            this->dirtyTexture[i] = false;
        }
    }

    /// configure fbo
    if (this->element->fbo != NULL && this->dirtyFbo)
    {
        this->element->fbo->bind();
        this->element->fbo->applyAttachment();
        this->element->fbo->unbind();
        this->dirtyFbo = false;
    }
    if (this->element->alternativeFbo != NULL && this->dirtyAlternativeFbo)
    {
        this->element->alternativeFbo->bind();
        this->element->alternativeFbo->applyAttachment();
        this->element->alternativeFbo->unbind();
        this->dirtyAlternativeFbo = false;
    }
}

void SceneNode::process()
{
    this->handleNodeProcessedEvent();
}

void SceneNode::setInheritParentTransformation(bool inherit)
{
    this->inheritParentTransformation = inherit;
}

bool SceneNode::getInheritParentTransformation() const
{
    return this->inheritParentTransformation;
}

void SceneNode::setReflect(bool reflect)
{
    this->reflect = reflect;
}

bool SceneNode::getReflect() const
{
    return this->reflect;
}

void SceneNode::addDrawable(Drawable& drawable)
{
    this->element->drawable = &drawable;
    this->dirtyDrawable = true;
    this->handleNodeModifiedEvent();
}

void SceneNode::addTexture(unsigned int unit, TextureObject& texture)
{
    if (unit >= this->element->texture.size())
    {
        int size = this->element->texture.size();
        for (unsigned int i = 0; i <= (unit - size); i++)
        {
            this->element->texture.push_back(&texture);
            this->dirtyTexture.push_back(true);
        }
    }

    this->element->texture[unit] = &texture;
    this->dirtyTexture[unit] = true;

    this->handleNodeModifiedEvent();
}

void SceneNode::setProgram(Program& program)
{
    this->element->program = &program;
    this->dirtyProgram = true;
    this->handleNodeModifiedEvent();
}

void SceneNode::setFrameBufferObject(FrameBufferObject& fbo)
{
    this->element->fbo = &fbo;
    this->dirtyFbo = true;
    this->handleNodeModifiedEvent();
}

void SceneNode::setAlternativeFrameBufferObject(FrameBufferObject& fbo)
{
    this->element->alternativeFbo = &fbo;
    this->dirtyAlternativeFbo = true;
    this->handleNodeModifiedEvent();
}

void SceneNode::addUniform(Uniform& uniform)
{
    this->element->uniformList.push_back(&uniform);
}

Uniform* SceneNode::getUniform(string name) const
{
    std::vector<Uniform*>::iterator it;
    for (it = this->element->uniformList.begin(); it != this->element->uniformList.end(); ++it)
    {
         if ((*it)->getName() == name)
         {
             return (*it);
         }
    }
    cout << "Error in SceneNode::getUniform : Requested Uniform doesnt exist" << endl;
    return NULL;
}

void SceneNode::setAlpha(float alpha)
{
    alpha = alpha > 1.0 ? 1.0 : alpha;
    alpha = alpha < 0.0 ? 0.0 : alpha;
    this->element->alpha = alpha;
}

float SceneNode::getAlpha() const
{
    return this->element->alpha;
}

void SceneNode::setPostProcess(bool postProcess)
{
    this->postProcess = postProcess;
}

bool SceneNode::getPostProcess() const
{
    return this->postProcess;
}

void SceneNode::translate(float x, float y, float z)
{
    /*this->element->transformations[0] = this->element->transformations[0] + this->element->transformations[3] * x;
    this->element->transformations[1] = this->element->transformations[1] + this->element->transformations[3] * y;
    this->element->transformations[2] = this->element->transformations[2] + this->element->transformations[3] * z;
    ///this->element->transformations[3] doesn't change
    this->element->transformations[4] = this->element->transformations[4] + this->element->transformations[7] * x;
    this->element->transformations[5] = this->element->transformations[5] + this->element->transformations[7] * y;
    this->element->transformations[6] = this->element->transformations[6] + this->element->transformations[7] * z;
    ///this->element->transformations[7] doesn't
    this->element->transformations[8] = this->element->transformations[8] + this->element->transformations[11] * x;
    this->element->transformations[9] = this->element->transformations[9] + this->element->transformations[11] * y;
    this->element->transformations[10] = this->element->transformations[10] + this->element->transformations[11] * z;
    ///this->element->transformations[11] doesn't change
    this->element->transformations[12] = this->element->transformations[12] + this->element->transformations[15] * x;
    this->element->transformations[13] = this->element->transformations[13] + this->element->transformations[15] * y;
    this->element->transformations[14] = this->element->transformations[14] + this->element->transformations[15] * z;
    ///this->element->transformations[15] doesn't change;*/

    /*this->element->transformations[12] = this->element->transformations[12] + this->element->transformations[0] * x
                                                                                    + this->element->transformations[4] * y
                                                                                    + this->element->transformations[8] * z;*/

    this->element->transformations(3, 0) = this->element->transformations(3, 0) + this->element->transformations(0, 0) * x +
                                                                                    this->element->transformations(1, 0) * y +
                                                                                    this->element->transformations(2, 0) * z;

    /*this->element->transformations[13] = this->element->transformations[13] + this->element->transformations[1] * x
                                                                                    + this->element->transformations[5] * y
                                                                                    + this->element->transformations[9] * z;*/

    this->element->transformations(3, 1) = this->element->transformations(3, 1) + this->element->transformations(0, 1) * x
                                                                                    + this->element->transformations(1, 1) * y
                                                                                    + this->element->transformations(2, 1) * z;

    /*this->element->transformations[14] = this->element->transformations[14] + this->element->transformations[2] * x
                                                                                    + this->element->transformations[6] * y
                                                                                    + this->element->transformations[10] * z;*/

    this->element->transformations(3, 2) = this->element->transformations(3, 2) + this->element->transformations(0, 2) * x
                                                                                    + this->element->transformations(1, 2) * y
                                                                                    + this->element->transformations(2, 2) * z;

    /*this->element->transformations[15] = this->element->transformations[15] + this->element->transformations[3] * x
                                                                                    + this->element->transformations[7] * y
                                                                                    + this->element->transformations[11] * z;*/

    this->element->transformations(3, 3) = this->element->transformations(3, 3) + this->element->transformations(0, 3) * x
                                                                                    + this->element->transformations(1, 3) * y
                                                                                    + this->element->transformations(2, 3) * z;
}

void SceneNode::scale(float x, float y, float z)
{
    /*this->element->transformations[0] = this->element->transformations[0] * x;
    this->element->transformations[1] = this->element->transformations[1] * y;
    this->element->transformations[2] = this->element->transformations[2] * z;
    ///this->element->transformations[3] doesn't change
    this->element->transformations[4] = this->element->transformations[4] * x;
    this->element->transformations[5] = this->element->transformations[5] * y;
    this->element->transformations[6] = this->element->transformations[6] * z;
    ///this->element->transformations[7] doesn't
    this->element->transformations[8] = this->element->transformations[8] * x;
    this->element->transformations[9] = this->element->transformations[9] * y;
    this->element->transformations[10] = this->element->transformations[10] * z;
    ///this->element->transformations[11] doesn't change
    this->element->transformations[12] = this->element->transformations[12] * x;
    this->element->transformations[13] = this->element->transformations[13] * y;
    this->element->transformations[14] = this->element->transformations[14] * z;
    ///this->element->transformations[15] doesn't change;*/
    this->element->transformations(0, 0) = this->element->transformations(0, 0) * x;
    this->element->transformations(0, 1) = this->element->transformations(0, 1) * y;
    this->element->transformations(0, 2) = this->element->transformations(0, 2) * z;
    ///this->element->transformations(0, 3) doesn't change
    this->element->transformations(1, 0) = this->element->transformations(1, 0) * x;
    this->element->transformations(1, 1) = this->element->transformations(1, 1) * y;
    this->element->transformations(1, 2) = this->element->transformations(1, 2) * z;
    ///this->element->transformations(1, 3) doesn't
    this->element->transformations(2, 0) = this->element->transformations(2, 0) * x;
    this->element->transformations(2, 1) = this->element->transformations(2, 1) * y;
    this->element->transformations(2, 2) = this->element->transformations(2, 2) * z;
    ///this->element->transformations(2, 3) doesn't change
    this->element->transformations(3, 0) = this->element->transformations(3, 0) * x;
    this->element->transformations(3, 1) = this->element->transformations(3, 1) * y;
    this->element->transformations(3, 2) = this->element->transformations(3, 2) * z;
    ///this->element->transformations(3, 3) doesn't change;
}

void SceneNode::rotateX(double theta)
{
    double costheta = cos(theta);
    double sintheta = sin(theta);

    /*float futureTransformation0 = this->element->transformations[4] * costheta + this->element->transformations[8] * sintheta;
    float futureTransformation1 = this->element->transformations[5] * costheta + this->element->transformations[9] * sintheta;
    float futureTransformation2 = this->element->transformations[6] * costheta + this->element->transformations[10] * sintheta;
    float futureTransformation3 = this->element->transformations[7] * costheta + this->element->transformations[11] * sintheta;

    float futureTransformation4 = this->element->transformations[8] * costheta - this->element->transformations[4] * sintheta;
    float futureTransformation5 = this->element->transformations[9] * costheta - this->element->transformations[5] * sintheta;
    float futureTransformation6 = this->element->transformations[10] * costheta - this->element->transformations[6] * sintheta;
    float futureTransformation7 = this->element->transformations[11] * costheta - this->element->transformations[7] * sintheta;*/
    float futureTransformation0 = this->element->transformations(1, 0) * costheta + this->element->transformations(2, 0) * sintheta;
    float futureTransformation1 = this->element->transformations(1, 1) * costheta + this->element->transformations(2, 1) * sintheta;
    float futureTransformation2 = this->element->transformations(1, 2) * costheta + this->element->transformations(2, 2) * sintheta;
    float futureTransformation3 = this->element->transformations(1, 3) * costheta + this->element->transformations(2, 3) * sintheta;

    float futureTransformation4 = this->element->transformations(2, 0) * costheta - this->element->transformations(1, 0) * sintheta;
    float futureTransformation5 = this->element->transformations(2, 1) * costheta - this->element->transformations(1, 1) * sintheta;
    float futureTransformation6 = this->element->transformations(2, 2) * costheta - this->element->transformations(1, 2) * sintheta;
    float futureTransformation7 = this->element->transformations(2, 3) * costheta - this->element->transformations(1, 3) * sintheta;

    /*this->element->transformations[4] = futureTransformation0;
    this->element->transformations[5] = futureTransformation1;
    this->element->transformations[6] = futureTransformation2;
    this->element->transformations[7] = futureTransformation3;

    this->element->transformations[8] = futureTransformation4;
    this->element->transformations[9] = futureTransformation5;
    this->element->transformations[10] = futureTransformation6;
    this->element->transformations[11] = futureTransformation7;*/
    this->element->transformations(1, 0) = futureTransformation0;
    this->element->transformations(1, 1) = futureTransformation1;
    this->element->transformations(1, 2) = futureTransformation2;
    this->element->transformations(1, 3) = futureTransformation3;

    this->element->transformations(2, 0) = futureTransformation4;
    this->element->transformations(2, 1) = futureTransformation5;
    this->element->transformations(2, 2) = futureTransformation6;
    this->element->transformations(2, 3) = futureTransformation7;

    /// this->element->transformations[0] doesn't change
    /*float futureTransformation0 = this->element->transformations[1] * costheta - this->element->transformations[2] * sintheta;
    float futureTransformation1 = this->element->transformations[2] * costheta + this->element->transformations[1] * sintheta;
    this->element->transformations[1] = futureTransformation0;
    this->element->transformations[2] = futureTransformation1;
    //this->element->transformations[1] = this->element->transformations[1] * costheta - this->element->transformations[2] * sintheta;
    //this->element->transformations[2] = this->element->transformations[2] * costheta + this->element->transformations[1] * sintheta;
    ///this->element->transformations[3] doesn't change
    /// this->element->transformations[4] doesn't change
    futureTransformation0 = this->element->transformations[5] * costheta - this->element->transformations[6] * sintheta;
    futureTransformation1 = this->element->transformations[6] * costheta + this->element->transformations[5] * sintheta;
    this->element->transformations[5] = futureTransformation0;
    this->element->transformations[6] = futureTransformation1;
    //this->element->transformations[5] = this->element->transformations[1] * costheta - this->element->transformations[2] * sintheta;
    //this->element->transformations[6] = this->element->transformations[2] * costheta + this->element->transformations[1] * sintheta;
    ///this->element->transformations[7] doesn't
    /// this->element->transformations[8] doesn't change
    futureTransformation0 = this->element->transformations[9] * costheta - this->element->transformations[10] * sintheta;
    futureTransformation1 = this->element->transformations[10] * costheta + this->element->transformations[9] * sintheta;
    this->element->transformations[9] = futureTransformation0;
    this->element->transformations[10] = futureTransformation1;
    //this->element->transformations[9] = this->element->transformations[1] * costheta - this->element->transformations[2] * sintheta;
    //this->element->transformations[10] = this->element->transformations[2] * costheta + this->element->transformations[1] * sintheta;
    ///this->element->transformations[11] doesn't change
    /// this->element->transformations[12] doesn't change
    futureTransformation0 = this->element->transformations[13] * costheta - this->element->transformations[14] * sintheta;
    futureTransformation1 = this->element->transformations[14] * costheta + this->element->transformations[13] * sintheta;
    this->element->transformations[13] = futureTransformation0;
    this->element->transformations[14] = futureTransformation1;
    //this->element->transformations[13] = this->element->transformations[1] * costheta - this->element->transformations[2] * sintheta;
    //this->element->transformations[14] = this->element->transformations[2] * costheta + this->element->transformations[1] * sintheta;
    ///this->element->transformations[15] doesn't change;*/
}

void SceneNode::rotateY(double theta)
{
    double costheta = cos(theta);
    double sintheta = sin(theta);
    /*float futureTransformation0 = this->element->transformations[0] * costheta - this->element->transformations[8] * sintheta;
    float futureTransformation1 = this->element->transformations[1] * costheta - this->element->transformations[9] * sintheta;
    float futureTransformation2 = this->element->transformations[2] * costheta - this->element->transformations[10] * sintheta;
    float futureTransformation3 = this->element->transformations[3] * costheta - this->element->transformations[11] * sintheta;

    float futureTransformation4 = this->element->transformations[8] * costheta + this->element->transformations[0] * sintheta;
    float futureTransformation5 = this->element->transformations[9] * costheta + this->element->transformations[1] * sintheta;
    float futureTransformation6 = this->element->transformations[10] * costheta + this->element->transformations[2] * sintheta;
    float futureTransformation7 = this->element->transformations[11] * costheta + this->element->transformations[3] * sintheta;*/

    float futureTransformation0 = this->element->transformations(0, 0) * costheta - this->element->transformations(2, 0) * sintheta;
    float futureTransformation1 = this->element->transformations(0, 1) * costheta - this->element->transformations(2, 1) * sintheta;
    float futureTransformation2 = this->element->transformations(0, 2) * costheta - this->element->transformations(2, 2) * sintheta;
    float futureTransformation3 = this->element->transformations(0, 3) * costheta - this->element->transformations(2, 2) * sintheta;

    float futureTransformation4 = this->element->transformations(2, 0) * costheta + this->element->transformations(0, 0) * sintheta;
    float futureTransformation5 = this->element->transformations(2, 1) * costheta + this->element->transformations(0, 1) * sintheta;
    float futureTransformation6 = this->element->transformations(2, 2) * costheta + this->element->transformations(0, 2) * sintheta;
    float futureTransformation7 = this->element->transformations(2, 3) * costheta + this->element->transformations(0, 3) * sintheta;

    /*this->element->transformations[0] = futureTransformation0;
    this->element->transformations[1] = futureTransformation1;
    this->element->transformations[2] = futureTransformation2;
    this->element->transformations[3] = futureTransformation3;

    this->element->transformations[8] = futureTransformation4;
    this->element->transformations[9] = futureTransformation5;
    this->element->transformations[10] = futureTransformation6;
    this->element->transformations[11] = futureTransformation7;*/

    this->element->transformations(0, 0) = futureTransformation0;
    this->element->transformations(0, 1) = futureTransformation1;
    this->element->transformations(0, 2) = futureTransformation2;
    this->element->transformations(0, 3) = futureTransformation3;

    this->element->transformations(2, 0) = futureTransformation4;
    this->element->transformations(2, 1) = futureTransformation5;
    this->element->transformations(2, 2) = futureTransformation6;
    this->element->transformations(2, 3) = futureTransformation7;

    /*this->element->transformations[0] = futureTransformation0;
    //this->element->transformations[0] = this->element->transformations[0] * costheta + this->element->transformations[2] * sintheta;
    /// this->element->transformations[1] doesn't change
    this->element->transformations[2] = futureTransformation1;
    //this->element->transformations[2] = this->element->transformations[2] * costheta - this->element->transformations[0] * sintheta;
    /// this->element->transformations[3] doesn't change
    futureTransformation0 = this->element->transformations[4] * costheta + this->element->transformations[6] * sintheta;
    futureTransformation1 = this->element->transformations[6] * costheta - this->element->transformations[4] * sintheta;
    this->element->transformations[4] = futureTransformation0;
    //this->element->transformations[4] = this->element->transformations[4] * costheta + this->element->transformations[6] * sintheta;
    /// this->element->transformations[5] doesn't change
    this->element->transformations[6] = futureTransformation1;
    //this->element->transformations[6] = this->element->transformations[6] * costheta - this->element->transformations[4] * sintheta;
    /// this->element->transformations[7] doesn't
    futureTransformation0 = this->element->transformations[8] * costheta + this->element->transformations[10] * sintheta;
    futureTransformation1 = this->element->transformations[10] * costheta - this->element->transformations[8] * sintheta;
    this->element->transformations[8] = futureTransformation0;
    //this->element->transformations[8] = this->element->transformations[8] * costheta + this->element->transformations[10] * sintheta;
    /// this->element->transformations[9] doesn't change
    this->element->transformations[10] = futureTransformation1;
    //this->element->transformations[10] = this->element->transformations[10] * costheta - this->element->transformations[8] * sintheta;
    /// this->element->transformations[11] doesn't change
    futureTransformation0 = this->element->transformations[12] * costheta + this->element->transformations[14] * sintheta;
    futureTransformation1 = this->element->transformations[14] * costheta - this->element->transformations[12] * sintheta;
    this->element->transformations[12] = futureTransformation0;
    //this->element->transformations[12] = this->element->transformations[12] * costheta + this->element->transformations[14] * sintheta;
    /// this->element->transformations[13] doesn't change;
    this->element->transformations[14] = futureTransformation1;
    //this->element->transformations[14] = this->element->transformations[14] * costheta - this->element->transformations[12] * sintheta;
    /// this->element->transformations[15] doesn't change;*/
}

void SceneNode::rotateZ(double theta)
{
    /*double costheta = cos(theta);
    double sintheta = sin(theta);
    float futureTransformation0 = this->element->transformations[0] * costheta - this->element->transformations[1] * sintheta;
    float futureTransformation1 = this->element->transformations[1] * costheta + this->element->transformations[0] * sintheta;
    this->element->transformations[0] = futureTransformation0;
    this->element->transformations[1] = futureTransformation1;
    //this->element->transformations[0] = this->element->transformations[0] * costheta - this->element->transformations[1] * sintheta;
    //this->element->transformations[1] = this->element->transformations[1] * costheta + this->element->transformations[0] * sintheta;
    /// this->element->transformations[2] doesn't change
    /// this->element->transformations[3] doesn't change
    futureTransformation0 = this->element->transformations[4] * costheta - this->element->transformations[5] * sintheta;
    futureTransformation1 = this->element->transformations[5] * costheta + this->element->transformations[4] * sintheta;
    this->element->transformations[4] = futureTransformation0;
    this->element->transformations[5] = futureTransformation1;
    //this->element->transformations[4] = this->element->transformations[4] * costheta - this->element->transformations[5] * sintheta;
    //this->element->transformations[5] = this->element->transformations[5] * costheta + this->element->transformations[4] * sintheta;
    /// this->element->transformations[6] doesn't change
    /// this->element->transformations[7] doesn't
    futureTransformation0 = this->element->transformations[8] * costheta - this->element->transformations[9] * sintheta;
    futureTransformation1 = this->element->transformations[9] * costheta + this->element->transformations[8] * sintheta;
    this->element->transformations[8] = futureTransformation0;
    this->element->transformations[9] = futureTransformation1;
    //this->element->transformations[8] = this->element->transformations[8] * costheta - this->element->transformations[9] * sintheta;
    //this->element->transformations[9] = this->element->transformations[9] * costheta + this->element->transformations[8] * sintheta;
    /// this->element->transformations[10] doesn't change
    /// this->element->transformations[11] doesn't change
    futureTransformation0 = this->element->transformations[12] * costheta - this->element->transformations[13] * sintheta;
    futureTransformation1 = this->element->transformations[13] * costheta + this->element->transformations[12] * sintheta;
    this->element->transformations[12] = futureTransformation0;
    this->element->transformations[13] = futureTransformation1;
    //this->element->transformations[12] = this->element->transformations[12] * costheta - this->element->transformations[13] * sintheta;
    //this->element->transformations[13] = this->element->transformations[13] * costheta + this->element->transformations[12] * sintheta;
    /// this->element->transformations[14] doesn't change
    /// this->element->transformations[15] doesn't change;*/


    double costheta = cos(theta);
    double sintheta = sin(theta);

    /*float futureTransformation0 = this->element->transformations[0] * costheta + this->element->transformations[4] * sintheta;
    float futureTransformation1 = this->element->transformations[1] * costheta + this->element->transformations[5] * sintheta;
    float futureTransformation2 = this->element->transformations[2] * costheta + this->element->transformations[6] * sintheta;
    float futureTransformation3 = this->element->transformations[3] * costheta + this->element->transformations[7] * sintheta;
    float futureTransformation4 = this->element->transformations[4] * costheta - this->element->transformations[0] * sintheta;
    float futureTransformation5 = this->element->transformations[5] * costheta - this->element->transformations[1] * sintheta;
    float futureTransformation6 = this->element->transformations[6] * costheta - this->element->transformations[2] * sintheta;
    float futureTransformation7 = this->element->transformations[7] * costheta - this->element->transformations[3] * sintheta;

    this->element->transformations[0] = futureTransformation0;
    this->element->transformations[1] = futureTransformation1;
    this->element->transformations[2] = futureTransformation2;
    this->element->transformations[3] = futureTransformation3;
    this->element->transformations[4] = futureTransformation4;
    this->element->transformations[5] = futureTransformation5;
    this->element->transformations[6] = futureTransformation6;
    this->element->transformations[7] = futureTransformation7;*/

    float futureTransformation0 = this->element->transformations(0, 0) * costheta + this->element->transformations(1, 0) * sintheta;
    float futureTransformation1 = this->element->transformations(0, 1) * costheta + this->element->transformations(1, 1) * sintheta;
    float futureTransformation2 = this->element->transformations(0, 2) * costheta + this->element->transformations(1, 2) * sintheta;
    float futureTransformation3 = this->element->transformations(0, 3) * costheta + this->element->transformations(1, 3) * sintheta;
    float futureTransformation4 = this->element->transformations(1, 0) * costheta - this->element->transformations(0, 0) * sintheta;
    float futureTransformation5 = this->element->transformations(1, 1) * costheta - this->element->transformations(0, 1) * sintheta;
    float futureTransformation6 = this->element->transformations(1, 2) * costheta - this->element->transformations(0, 2) * sintheta;
    float futureTransformation7 = this->element->transformations(1, 3) * costheta - this->element->transformations(0, 3) * sintheta;

    this->element->transformations(0, 0) = futureTransformation0;
    this->element->transformations(0, 1) = futureTransformation1;
    this->element->transformations(0, 2) = futureTransformation2;
    this->element->transformations(0, 3) = futureTransformation3;
    this->element->transformations(1, 0) = futureTransformation4;
    this->element->transformations(1, 1) = futureTransformation5;
    this->element->transformations(1, 2) = futureTransformation6;
    this->element->transformations(1, 3) = futureTransformation7;



}


}
